
import java.io.IOException;
import java.util.Enumeration;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author khang
 */
public class PlayCanvas extends GameCanvas implements Runnable,CommandListener{
    private Touhou midlet;
    private Command cmExit=new Command("Return to main menu", Command.EXIT, 1);
    
    private GameDesign Gamedesign;
    private int iWidth,iHeight;
    private LayerManager Layermanager,Layermanager2;
    private Sprite player;
    private boolean isActivate=false;
    private myPlayer MyPlayer;
    
    private boolean isConversation,startConversation,isMenu,isHelp, isAbout;
    
    private int iMenu;
    private Sound sound;
    
    Image imgBG_window;
    Image imgBG_house;
    
    
    //
   
    
    //menu skill
    private MenuFuntionItem skill;
    private MenuFuntionItem back;
    
    public PlayCanvas(Touhou midlet){
        super(true);
        setFullScreenMode(true);
        addCommand(cmExit);
        setCommandListener(this);
        this.midlet=midlet;
    }
    public void gameStart(){
        Gamedesign=new GameDesign();
        Layermanager=new LayerManager();  
        Layermanager2=new LayerManager();  
        sound=new Sound();
        iWidth=getWidth();
        iHeight=getHeight();
        
        isMenu=true;
        isHelp=false;
        isAbout=false;
        
        
        iMenu=0;
        
        
        try {            
           
            //player=Gamedesign.getPlayer();
            
           
            MyPlayer=new myPlayer(Layermanager,sound, iWidth, iHeight);
            
            Gamedesign.updateLayerManagerForMenu_scene(Layermanager2);
            //menu funtion item
            int size = 40;
            skill = new MenuFuntionItem("/fart_button.png", 0, iHeight - 2*size-5, size, size,"", 0, 0, true);
            back = new MenuFuntionItem("/back.png", 0, iHeight - size, size, size,"", 0, 0, true);
            
            imgBG_window=Image.createImage("/window1.png");
            imgBG_house=Image.createImage("/house.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        

        isActivate=true;
        Thread t=new Thread(this);
        t.start();
    }

    public void run() {
        while(isActivate){
            updateWorld();
            drawWorld();
            flushGraphics();
            CheckMenmory();
   
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    protected void pointerPressed(int arg0, int arg1) {
    	// TODO Auto-generated method stub
    	if(!isMenu)
    	{
    		if(back.IsClick(arg0, arg1))
    		{
    			isMenu = true;
    			MyPlayer.disableCat();
    		}
    		if(skill.IsClick(arg0, arg1))
    		{
    			MyPlayer.disableCat();
    			MyPlayer.enableBigFart();
    			sound.bigFartSound();
    		}
    		
    		if(arg0>=90&&arg0<=150&&arg1>=150&&arg1<=200)
    		{
    			MyPlayer.disableCat();
    			int check = MyPlayer.enableHitHead();
    			if(check>=3)
    			{
    				MyPlayer.disableCat();
        			MyPlayer.enableKnockOut();
    			}
    			sound.slapSound();
    		}
    		if(arg0>=105&&arg0<=135&&arg1>=210&&arg1<=255)
    		{
    			MyPlayer.disableCat();
    			MyPlayer.enableHitStomach();
    			sound.slapSound();
    		}
    		if(arg0>=100&&arg0<=115&&arg1>=260&&arg1<=285)
    		{
    			MyPlayer.disableCat();
    			MyPlayer.enableHitRightFoot();
    			sound.slapSound();
    		}
    		if(arg0>=123&&arg0<=138&&arg1>=260&&arg1<=285)
    		{
    			MyPlayer.disableCat();
    			MyPlayer.enableHitLeftFoot();
    			sound.slapSound();
    		}

    	}
    	else
    	{//iWidth/2 - 30, iHeight/2-40, 45, 20
    		if((arg0 > (iWidth/2 - 30)) && (arg0 < (iWidth/2+15)) && (arg1 > (iHeight/2-40)) && (arg1 < (iHeight/2-20)))
    		{
    			isMenu = false;
    			MyPlayer.enableIdle();
    		}
    		if(arg0 > iWidth/2 - 30 && arg0 < iWidth/2+15 && arg1 > iHeight/2-15 && arg1 < iHeight/2+5)
    			isHelp=true;
    		if(arg0 > iWidth/2 - 30 && arg0 < iWidth/2+15 && arg1 > iHeight/2+10 && arg1 < iHeight/2+30)
    			isAbout=true;
    		if(arg0 > iWidth/2 - 30 && arg0 < iWidth/2+15 && arg1 > iHeight/2+35 && arg1 < iHeight/2+55)
    			midlet.Exit();
    		if(isAbout&&back.IsClick(arg0, arg1))
    		{
    			isAbout=false;
    		}
    		if(isHelp&&back.IsClick(arg0, arg1))
    		{
    			isHelp=false;
    		}
    	}
    }
    
    protected void pointerReleased(int arg0, int arg1) {
    	// TODO Auto-generated method stub
    	
    }
    
    
        
    
    private  void updateWorld(){
        if(!isMenu)
        {
        	int check = MyPlayer.CatMove();
        	if(check==0)
        	{
        		MyPlayer.disableCat();
    			MyPlayer.enableIdle();
        	}
            
        }
        else if(isMenu){
            //sound.bakcgroundSoundStop();
        }

    }
    private void drawWorld(){
        Graphics g=getGraphics();
      //  (iWidth/2 - 30)) && (arg0 < (iWidth/2)) && (arg1 > (iHeight/2-40)) && (arg1 < (iHeight/2-10)))
        if(!isMenu){
        	
        	
            //Layermanager.setViewWindow(scrX, scrY, iWidth, iHeight);
        	g.drawImage(imgBG_house, 0, 0, 0);
            Layermanager.paint(g, 0, 0);
            g.drawImage(imgBG_window, 50, 90, 0);
            //menu funtion item
            skill.Draw(g, 0);
            back.Draw(g, 0);
        }
        else if(isMenu&&!isHelp&&!isAbout)
        {
            Layermanager2.paint(g, 0, 0);
            if(iMenu==0){
                g.setColor(255, 0, 0);
                g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
            }
            else{
                g.setColor(255, 0, 0);
                g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            }
            g.drawString("Start", iWidth/2 - 30, iHeight/2 - 40, Graphics.TOP|Graphics.LEFT);
            
            g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("Help", iWidth/2 - 30, iHeight/2 - 15, Graphics.TOP|Graphics.LEFT);
            
            g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("About", iWidth/2 - 30, iHeight/2 + 10, Graphics.TOP|Graphics.LEFT);
            
            if(iMenu==1){
                g.setColor(255, 0, 0);
                g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
            }
            else{
                g.setColor(255, 0, 0);
                g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            }
            g.drawString("Quit", iWidth/2 -30, iHeight/2 +35, Graphics.TOP|Graphics.LEFT);
        }
        else if(isHelp)
        {
        	Layermanager2.paint(g, 0, 0);
        	back.Draw(g, 0);
        	g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("HELP", 100, 80, Graphics.TOP|Graphics.LEFT);
            g.setColor(255, 255, 255);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
            g.drawString("Touch the screen to move your", 10, 130, Graphics.TOP|Graphics.LEFT);
            g.drawString("ship to your finger's direction", 10, 150, Graphics.TOP|Graphics.LEFT);
            g.drawString("Eat power's dropped by enemy's", 10, 180, Graphics.TOP|Graphics.LEFT);
            g.drawString("ship to level up your fire power ", 10, 200, Graphics.TOP|Graphics.LEFT);
            g.drawString("Touch the \"skill\" icon to use skill", 10, 230, Graphics.TOP|Graphics.LEFT);
            g.drawString("and blow up all enemy", 10, 250, Graphics.TOP|Graphics.LEFT);
        }
        else if(isAbout)
        {
        	Layermanager2.paint(g, 0, 0);
        	g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("ABOUT", 100, 80, Graphics.TOP|Graphics.LEFT);
        	
        	g.setColor(255, 255, 255);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
            g.drawString("Developer Name: Khang", 30, 130, Graphics.TOP|Graphics.LEFT);
            g.drawString("Application Name: Battleship", 30, 180, Graphics.TOP|Graphics.LEFT);
            g.drawString("Version: 1.0", 30, 230, Graphics.TOP|Graphics.LEFT);
        	
        	back.Draw(g, 0);
        }
    }
    //phia tren tu 2 phia
    
    //phia tren tu ben trai
    
    
    public void commandAction(Command c, Displayable d) {
        if(c==cmExit){
            isMenu=true;
        }
    }
    static public void CheckMenmory()
    {
        Runtime runtime = Runtime.getRuntime();
        runtime.gc();
        
        runtime = null;
    }
    
    
    
    
}
